/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.boot.environment.impl;

import java.util.Map;
import java.io.*;
import com.jspx.boot.environment.Placeholder;
import com.jspx.scriptmark.config.TemplateConfigurable;
import com.jspx.scriptmark.Configurable;
import com.jspx.scriptmark.ScriptmarkEnv;
import com.jspx.scriptmark.ScriptMark;
import com.jspx.scriptmark.load.StringSource;
import com.jspx.scriptmark.load.FileSource;
import com.jspx.scriptmark.core.ScriptMarkEngine;
import com.jspx.security.utils.EncryptUtil;
import com.jspx.utils.DateUtil;
import com.jspx.utils.FileUtil;
import com.jspx.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by IntelliJ IDEA.
 * User:chenYuan (mail:39793751@qq.com)
 * Date: 2007-2-27
 * Time: 16:22:16
 * com.jspx.impl.environment.impl.FreemakerTemplate
 */
public class PlaceholderImpl implements Placeholder {
    final static private Logger log = LoggerFactory.getLogger(PlaceholderImpl.class);
    private String currentPath = FileUtil.mendPath(System.getProperty("user.dir")); //当前路径，f方便include 使用
    private String rootDirectory = FileUtil.mendPath(System.getProperty("user.dir")); //路径范围
    final static private Configurable configuration = new TemplateConfigurable();

    static {
        configuration.put(ScriptmarkEnv.NumberFormat, "###0.######");
        configuration.put(ScriptmarkEnv.DateFormat, DateUtil.DAY_FORMAT);
        configuration.put(ScriptmarkEnv.DateTimeFormat, "yyyy-MM-dd HH:mm");
        configuration.put(ScriptmarkEnv.TimeFormat, "HH:mm");
        configuration.put(ScriptmarkEnv.Template_update_delay, 3600);
        configuration.put(ScriptmarkEnv.MacroCallTag, "@");
        configuration.put(ScriptmarkEnv.Language, "JavaScript");
        configuration.put(ScriptmarkEnv.Syncopate, "<>");
        configuration.put(ScriptmarkEnv.VariableBegin, "${");
        configuration.put(ScriptmarkEnv.VariableEnd, "}");
        configuration.put(ScriptmarkEnv.VariableBegin, "${");
        configuration.put(ScriptmarkEnv.escapeVariable, "\\");
    }

    public PlaceholderImpl() {

    }

    /**
     *
     *
     * @param valueMap
     * @param templateString 字符方式
     * @return
     * @throws Exception
     */
    public String processTemplate(Map<String, Object> valueMap, String templateString) {
        ScriptMark scriptMark = null;
        try {
            scriptMark = new ScriptMarkEngine(ScriptmarkEnv.noCache, new StringSource(templateString), configuration);
            Writer writer = new StringWriter();
            scriptMark.process(writer, valueMap);
            writer.close();
            return writer.toString();
        } catch (Exception e) {
            log.error(templateString ,e);
        }
        return StringUtil.empty;
    }

    /**
     *
     *
     * @param valueMap
     * @param file 文件方式
     * @param encode
     * @return
     * @throws Exception
     */
    public String processTemplate(Map<String, Object> valueMap, File file, String encode) throws Exception {
        FileSource fs = new FileSource(file, file.getName(), encode);
        ScriptMark scriptMark = new ScriptMarkEngine(EncryptUtil.getMd5(file.getAbsolutePath()), fs, configuration);
        Writer writer = new StringWriter();
        scriptMark.process(writer, valueMap);
        writer.close();
        return writer.toString();
    }

    public String getCurrentPath() {
        return currentPath;
    }

    public void setCurrentPath(String currentPath) {
        this.currentPath = currentPath;
    }

    public String getRootDirectory() {
        return rootDirectory;
    }

    public void setRootDirectory(String rootDirectory) {
        this.rootDirectory = rootDirectory;
    }
}